/**
 * Module dependencies.
 */

var express = require('express');
var logger = require('morgan');
var session = require('express-session');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');

var app = module.exports = express();

// settings
// set our default template engine to "jade"
// which prevents the need for extensions
app.set('view engine', 'jade');
// set views for error and 404 pages
app.set('views', __dirname + '/views'); 

// log
if (!module.parent)
	app.use(logger('dev'));

// 静态目录
app.use(express.static(__dirname + '/public'));

// parse request bodies (req.body)
app.use(bodyParser.urlencoded({
	extended : true
}));

// allow overriding methods in query (?_method=put)
app.use(methodOverride('_method'));

// load controllers
require('./lib/boot')(app, {
	verbose : !module.parent,
	defaultController : 'main',
	defaultAction : 'index'
});

app.use(function(err, req, res, next) {
	// log it
	if (!module.parent)
		console.error(err.stack);
	// error page
	res.status(500).render('5xx');
});

// assume 404 since no middleware responded
app.use(function(req, res, next) {
	res.status(404).render('404', {
		url : req.originalUrl
	});
});

/* istanbul ignore next */
if (!module.parent) {
	app.listen(3000);
	console.log('started on port 3000');
}
